import request from '@/utils/request'

// 查询消息推送列表
export function listPush(query) {
  return request({
    url: '/ry/push/push/list',
    method: 'get',
    params: query
  })
}

// 查询消息推送详细
export function getPush(id) {
  return request({
    url: '/ry/push/push/' + id,
    method: 'get'
  })
}

// 新增消息推送
export function addPush(data) {
  return request({
    url: '/ry/push/push',
    method: 'post',
    data: data
  })
}

// 修改消息推送
export function updatePush(data) {
  return request({
    url: '/ry/push/push',
    method: 'put',
    data: data
  })
}

// 删除消息推送
export function delPush(id) {
  return request({
    url: '/ry/push/push/' + id,
    method: 'delete'
  })
}

// 确认单个消息推送
export function confirmPush(id) {
  return request({
    url: '/ry/push/push/confirm/' + id,
    method: 'put'
  })
}

// 批量确认消息推送
export function confirmPushBatch(ids) {
  return request({
    url: '/ry/push/push/confirmBatch',
    method: 'put',
    data: ids
  })
}

// 添加获取联系人列表的API方法
export function listContact(keyword) {
  return request({
    url: '/ry/push/push/contactList',
    method: 'get',
    params: { keyword }
  })
}

// AI聊天接口
export async function chatAI(data, signal) {
  const token = localStorage.getItem('Admin-Token') || '';
  const response = await fetch(process.env.VUE_APP_BASE_API + '/push/AI/chat', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + token
    },
    body: JSON.stringify(data),
    signal: signal
  });

  if (!response.body || response.status !== 200) {
    throw new Error('AI接口异常或未返回流式内容');
  }

  return response.body.getReader();
}

// AI分析客户接口
export async function analyzeCustomerAI(customerId, modelName, signal) {
  const token = localStorage.getItem('Admin-Token') || '';
  const response = await fetch(process.env.VUE_APP_BASE_API + '/push/AI/analyze?' + new URLSearchParams({
    customerId: customerId,
    modelName: modelName
  }), {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + token
    },
    signal: signal
  });

  if (!response.body || response.status !== 200) {
    throw new Error('AI接口异常或未返回流式内容');
  }

  return response.body.getReader();
}
